package org.sundatagroup.aurora.monitor.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.sundatagroup.aurora.core.service.querier.IQuerier;
import org.sundatagroup.aurora.monitor.dao.PlayListDao;
import org.sundatagroup.aurora.monitor.entity.command.PlayerCommand;
import org.sundatagroup.aurora.monitor.entity.command.SwitchProjectCommand;
import org.sundatagroup.aurora.monitor.entity.model.PlayListModel;
import org.sundatagroup.aurora.monitor.entity.pojo.PlayList;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * <p>播放列表的相关服务
 * </p>
 * 
 * @version 0.2.0
 * <br>补充通用查询接口
 * 
 * @author CharlesChen
 * @author Zdream
 * @date 2018-05-30
 * @since 0.1.1
 */
@Service
public class PlayListService implements IQuerier {
	
	@Autowired
	PlayListDao playListDao;
	
	@Autowired
	ScreenBatches batches;
	
	/* **********
	 * 增删查改 *
	 ********** */
	
	/**
	 * 增操作：增加一个{@link PlayListModel}
	 * @param p
	 * @return
	 */
	public boolean add(PlayList p) {
		if (p == null || !p.isValid()) {
			return false;
		}
		PlayListModel pm = p.toModel();
		playListDao.add(pm);

		return true;
	}

	/**
	 * 查操作：根据{@link PlayListModel}的name查找
	 * @param name
	 * @return
	 */
	public PlayList queryByName(String name) {
		PlayListModel pm = playListDao.findByName(name);
		if (pm == null) {
			return null;
		}
		PlayList p = PlayList.fromModel(pm);

		return p;
	}
	/**
	 * 查操作：根据{@link PlayListModel}的lid查找
	 * @param lid
	 * @return
	 */
	public PlayList queryByLid(int lid) {
		PlayListModel pm = playListDao.findByLid(lid);
		if (pm == null) {
			return null;
		}
		PlayList p = PlayList.fromModel(pm);

		return p;
	}

	/**
	 * 返回所有的{@link PlayList}
	 * @return
	 */
	public List<PlayList> queryAll() {
		List<PlayListModel> playListModels = playListDao.findAll();
		if (playListModels == null) {
			return null;
		}
		List<PlayList> playLists = new ArrayList<>(playListModels.size());
		for (PlayListModel pm : playListModels) {
			PlayList p = PlayList.fromModel(pm);
			playLists.add(p);
		}

		return playLists;
	}

	/**
	 * 查操作：查询{@link PlayListModel}在数据库的数量
	 * @return
	 */
	public int countPlayList() {
		return playListDao.count();
	}
	
	/* **********
	 * 播放状态 *
	 ********** */
	/*
	 * 与展示状态（展示区）不同
	 */
	
	/**
	 * 指派指定的展示屏播放指定的播放列表
	 * <p>注意, 如果需要更新展示屏状态并发布给监控区前端时,
	 * 请通过 {@link MonitorService#updateScreenPlayerStatus(String)} 来调用
	 * </p>
	 * @param sid
	 *   屏幕 id
	 * @param lid
	 *   播放列表 id
	 */
	public void assignPlayList(String sid, int lid) {
		batches.updatePlayList(sid, this.queryByLid(lid));
	}
	
	/**
	 * 播放下一个工程
	 * @param sid
	 *   屏幕 id
	 * @return
	 *   返回跳转工程的指令.
	 *   如果没有下一个要播放的工程, 返回 null.
	 */
	public SwitchProjectCommand playNext(String sid) {
		int pid = batches.getPlayerParam(sid).chooseNextProject();
		
		if (pid == -1) {
			return null;
		}
		
		return PlayerCommand.switchToProject(pid);
	}
	
	/* **********
	 * 通用查询 *
	 ********** */

	@Override
	public String[] types() {
		return new String[] {"/query/playList"};
	}

	/**
	 * 允许 <code>"/query/playList"</code> 查询数据:
	 * <li>如果含 id 键, 按 id 进行查询.
	 * <li>如果不含 id 键, 查询所有播放列表.
	 * </li>
	 */
	@Override
	public JsonElement query(String type, JsonElement key) {
		if (key.isJsonObject()) {
			JsonObject keyo = key.getAsJsonObject();
			if (keyo.has("id")) {
				// 查询单个
				PlayList p = this.queryByLid(keyo.get("id").getAsInt());
				if (p != null) {
					return p.toJson();
				}
			} else {
				// 查询多个
				List<PlayList> list = queryAll();
				JsonArray array = new JsonArray(list.size());
				list.forEach((p) -> {array.add(p.toJson());});
				return array;
			}
		}
		
		return null;
	}

}
